<?php
namespace JsonRpc\Utils;
use JsonRpc\Models\Admin;

/**
 *  用户
 * @author walkor <worker-man@qq.com>
 */
class Utils
{
    /**
     * 字符串加密
     * @param $str
     * @return string
     */
    public static function encrypt($str)
    {
        return md5($str);
    }

    /**
     * 字符串解密比较
     * @param $str
     * @param $encrypt_str
     * @return bool
     */
    public static function encryptVerifiy($str, $encrypt_str){
        return md5($str) == $encrypt_str;
    }

    /**
     * 列表转树表格
     * @param $list 数据列表
     * @param string $pk 主键字段
     * @param string $pid 关联父字段
     * @param string $child 子结点名称
     * @param int $root 第一层父ID值
     * @return array
     */
    public static function make_treeTable($list, $pk='id', $pid='pid', $child='_child', $root=0)
    {
        $tree=array();
        $packData=array();

        //转换数组的结构
        foreach ($list as $data) {
            $packData[$data[$pk]] = $data;
        }

        foreach ($packData as $key =>$val){
            if($val[$pid]==$root){//代表跟节点
                $tree[]=& $packData[$key];
            }else{
                //找到其父类
                $packData[$val[$pid]][$child][]=& $packData[$key];
            }
        }
        return $tree;
    }

    public static function make_tree($list, $pk='id', $pid='pid', $text='name', $root=0, $is_open = false, $checked_id = 0)
    {
        $tree=array();
        $packData=array();

        //转换数组的结构
        foreach ($list as $data) {
            $packData[$data[$pk]] = [
                'id'=>$data[$pk],
                'text'=>$data[$text],
                'state'=>$is_open?'open':'closed',
                //'checked'=>$data[$pk]==$checked_id?true:false,
                'attributes'=>$data
            ];
        }

        foreach ($packData as $key =>$val){
            if($val['attributes'][$pid]==$root){//代表跟节点
                $tree[]=& $packData[$key];
            }else{
                //找到其父类
                $packData[$val['attributes'][$pid]]['children'][]=& $packData[$key];
            }
        }
        return $tree;
    }

    public static function createSn($params)
    {
        $str = $params['id'].time().rand(5);
        return $str;
    }
}
